package com.emr.web.controller.emr;


import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emr.common.annotation.Log;
import com.emr.common.base.AjaxResult;
import com.emr.common.enums.BusinessType;
import com.emr.framework.web.page.TableDataInfo;
import com.emr.module.domain.Personal;
import com.emr.module.domain.Zhikong;
import com.emr.module.service.IIcdService;
import com.emr.module.service.IPersonalService;
import com.emr.module.service.IZhikongService;
import com.emr.system.domain.SysUser;
import com.emr.web.core.base.BaseController;

/**
 * 病历质控 信息操作处理
 * 
 * @author emr
 * @date 2019-03-01
 */
@Controller
@RequestMapping("/module/zhikong")
public class ZhikongController extends BaseController
{
    private String prefix = "module/zhikong";
	
	@Autowired
	private IZhikongService zhikongService;
	@Autowired
	private IIcdService icdService;
	@Autowired
	private IPersonalService ipersonalService;
	
	@RequiresPermissions("module:zhikong:view")
	@GetMapping()
	public String zhikong()
	{
	    return prefix + "/zhikong";
	}
	
	/**
	 * 查询病历质控列表
	 */
	@RequiresPermissions("module:zhikong:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Zhikong zhikong)
	{
		startPage();
        List<Zhikong> list = zhikongService.selectZhikongList(zhikong);
		return getDataTable(list);
	}
	
	/**
	 * 新增病历质控
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存病历质控
	 */
	@RequiresPermissions("module:zhikong:add")
	@Log(title = "病历质控", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Zhikong zhikong)
	{		
		return toAjax(zhikongService.insertZhikong(zhikong));
	}

	/**
	 * 修改病历质控
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		SysUser user = getUser();
		Zhikong zhikong = zhikongService.selectZhikongById(id);
		mmap.put("posts", icdService.selectIcdByIds(zhikong.getIcdCode()));
		zhikong.setZhikongRen(user.getUserName());
		mmap.put("zhikong", zhikong);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存病历质控
	 */
	@RequiresPermissions("module:zhikong:edit")
	@Log(title = "病历质控", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Zhikong zhikong,Personal personal)
	{		
		SysUser user = getUser();
		zhikong.setZhikongRen(user.getUserName());
		int updateZhikong = zhikongService.updateZhikong(zhikong);
		personal.setZhikongsfTG(zhikong.getShYesOnNo());
		personal.setButongguoYY(zhikong.getNoNeirong());
		personal.setId(zhikong.getPertiendId());
		ipersonalService.updatePersonal(personal);
		return toAjax(updateZhikong);
	}
	
	/**
	 * 删除病历质控
	 */
	@RequiresPermissions("module:zhikong:remove")
	@Log(title = "病历质控", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(zhikongService.deleteZhikongByIds(ids));
	}
	
}
